//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "LfmSwaptionEngine.h"
using namespace Cephei::QL::Legacy::Libormarketmodels;
#include <gen/QL/Legacy/Libormarketmodels/LiborForwardModel.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/PricingEngine.h>
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Legacy::Libormarketmodels::CLfmSwaptionEngine::CLfmSwaptionEngine (Cephei::QL::Legacy::Libormarketmodels::ILiborForwardModel^ model, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve) : CPricingEngine(CLfmSwaptionEngine::typeid)
{
    CLiborForwardModel^ _Cmodel;
    CYieldTermStructure^ _CdiscountCurve;
    try
    {
#ifdef HANDLE
        _phLfmSwaptionEngine = NULL;
#endif
        _Cmodel = safe_cast<CLiborForwardModel^> (model);
        _Cmodel->Lock();
        boost::shared_ptr<QuantLib::LiborForwardModel>& _model = static_cast<boost::shared_ptr<QuantLib::LiborForwardModel>&> (_Cmodel->GetShared ()); 
        _CdiscountCurve = safe_cast<CYieldTermStructure^> (discountCurve);
        _CdiscountCurve->Lock();
        Handle<QuantLib::YieldTermStructure>& _discountCurve = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdiscountCurve->GetHandle ()); 
        _ppLfmSwaptionEngine = new boost::shared_ptr<QuantLib::LfmSwaptionEngine> (new QuantLib::LfmSwaptionEngine ( _model,  _discountCurve ));
        SetPricingEngine (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppLfmSwaptionEngine));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmodel != nullptr) _Cmodel->Unlock();
        if (_CdiscountCurve != nullptr) _CdiscountCurve->Unlock();
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLfmSwaptionEngine::CLfmSwaptionEngine (boost::shared_ptr<QuantLib::LfmSwaptionEngine>& childNative, Object^ owner) : CPricingEngine(CLfmSwaptionEngine::typeid)
{
#ifdef HANDLE
	_phLfmSwaptionEngine = NULL;
#endif
	_ppLfmSwaptionEngine = &childNative;
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppLfmSwaptionEngine));
}
Cephei::QL::Legacy::Libormarketmodels::CLfmSwaptionEngine::CLfmSwaptionEngine (QuantLib::LfmSwaptionEngine& childNative, Object^ owner) : CPricingEngine(CLfmSwaptionEngine::typeid)
{
#ifdef HANDLE
	_phLfmSwaptionEngine = NULL;
#endif
	_ppLfmSwaptionEngine = new boost::shared_ptr<QuantLib::LfmSwaptionEngine> (&childNative);
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppLfmSwaptionEngine));
    _LfmSwaptionEngineOwner = owner;
    _PricingEngineOwner = owner;
}

Cephei::QL::Legacy::Libormarketmodels::CLfmSwaptionEngine::CLfmSwaptionEngine (CLfmSwaptionEngine^ copy) : CPricingEngine(CLfmSwaptionEngine::typeid)
{
#ifdef HANDLE
	_phLfmSwaptionEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLfmSwaptionEngine = new boost::shared_ptr<QuantLib::LfmSwaptionEngine> (copy->GetShared());
        _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppLfmSwaptionEngine));
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLfmSwaptionEngine::CLfmSwaptionEngine (System::Type^ t) : CPricingEngine(CLfmSwaptionEngine::typeid)
{
#ifdef HANDLE
	_phLfmSwaptionEngine = NULL;
#endif
	if (!t->IsSubclassOf(CLfmSwaptionEngine::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Legacy::Libormarketmodels::CLfmSwaptionEngine::CLfmSwaptionEngine (QuantLib::Handle<QuantLib::LfmSwaptionEngine>& childNative, Object^ owner)  : CPricingEngine(CLfmSwaptionEngine::typeid)
{
	_phLfmSwaptionEngine = &childNative;
	_ppLfmSwaptionEngine = &static_cast<boost::shared_ptr<QuantLib::LfmSwaptionEngine>>(childNative.currentLink());
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppLfmSwaptionEngine));
    _LfmSwaptionEngineOwner = owner;
}
Cephei::QL::Legacy::Libormarketmodels::CLfmSwaptionEngine::CLfmSwaptionEngine (QuantLib::Handle<QuantLib::LfmSwaptionEngine> childNative)  : CPricingEngine(CLfmSwaptionEngine::typeid)
{
	_phLfmSwaptionEngine = &childNative;
	_ppLfmSwaptionEngine = &static_cast<boost::shared_ptr<QuantLib::LfmSwaptionEngine>>(childNative.currentLink());
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppLfmSwaptionEngine));
}
#endif
#ifdef STRUCT
Cephei::QL::Legacy::Libormarketmodels::CLfmSwaptionEngine::CLfmSwaptionEngine (QuantLib::LfmSwaptionEngine childNative)  : CPricingEngine(CLfmSwaptionEngine::typeid)
{
#ifdef HANDLE
	_phLfmSwaptionEngine = NULL;
#endif
	_ppLfmSwaptionEngine = new boost::shared_ptr<QuantLib::LfmSwaptionEngine> (new QuantLib::LfmSwaptionEngine (childNative));
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppLfmSwaptionEngine));
}
#endif

Cephei::QL::Legacy::Libormarketmodels::CLfmSwaptionEngine::~CLfmSwaptionEngine ()
{
    if (_ppLfmSwaptionEngine != NULL)
    {
	    delete _ppLfmSwaptionEngine;
        _ppLfmSwaptionEngine = NULL;
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLfmSwaptionEngine::!CLfmSwaptionEngine ()
{
    if (_ppLfmSwaptionEngine != NULL)
    {
	    delete _ppLfmSwaptionEngine;
    }
}
QuantLib::LfmSwaptionEngine& Cephei::QL::Legacy::Libormarketmodels::CLfmSwaptionEngine::GetReference ()
{
    if (_ppLfmSwaptionEngine == NULL) throw gcnew NativeNullException ();
	return **_ppLfmSwaptionEngine;
}
boost::shared_ptr<QuantLib::LfmSwaptionEngine>& Cephei::QL::Legacy::Libormarketmodels::CLfmSwaptionEngine::GetShared ()
{
    if (_ppLfmSwaptionEngine == NULL) throw gcnew NativeNullException ();
	return *_ppLfmSwaptionEngine;
}
QuantLib::LfmSwaptionEngine* Cephei::QL::Legacy::Libormarketmodels::CLfmSwaptionEngine::GetPointer ()
{
    if (_ppLfmSwaptionEngine == NULL) throw gcnew NativeNullException ();
	return &**_ppLfmSwaptionEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LfmSwaptionEngine>& Cephei::QL::Legacy::Libormarketmodels::CLfmSwaptionEngine::GetHandle ()
{
	if (_phLfmSwaptionEngine == NULL)
	{
		_phLfmSwaptionEngine = new Handle<QuantLib::LfmSwaptionEngine> (*_ppLfmSwaptionEngine);
	}
	return *_phLfmSwaptionEngine;
}
#endif
bool Cephei::QL::Legacy::Libormarketmodels::CLfmSwaptionEngine::HasNative () 
{
	return (_ppLfmSwaptionEngine != NULL);
}

Cephei::QL::Legacy::Libormarketmodels::ILfmSwaptionEngine^ Cephei::QL::Legacy::Libormarketmodels::CLfmSwaptionEngine::Calculate::get ()
{
    try
    {
    	(*_ppLfmSwaptionEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Legacy::Libormarketmodels::ILfmSwaptionEngine^ Cephei::QL::Legacy::Libormarketmodels::CLfmSwaptionEngine_Factory::Create (Cephei::QL::Legacy::Libormarketmodels::ILiborForwardModel^ model, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve)
{
    return gcnew CLfmSwaptionEngine ( model,  discountCurve);
}
